package Domain.forumProtcol;

import java.util.Vector;

import objects.Frm;
import objects.Message;
import objects.Trd;
import objects.User;
import Domain.forumManager.ForumController;
import Domain.stompProtocol.ForumFrame;
import Domain.stompProtocol.StompClient;
import Domain.stompProtocol.StompFrame;

public class GettersProtocol extends ProtocolHelper
{
	public GettersProtocol(StompClient inStompClient,ForumController f,ForumFrame frmFrame)
	{
		this._stompClient = inStompClient;
		this.forumController=f;
		this.forumfrm=frmFrame;
	}
	
	public String processMessage(StompFrame msg)
	{
		String ans="";
		String str = msg.getData();
		String nameOfUser = msg.getUser();
		
		 if (msg.getData().startsWith("get users"))
		{
			Vector<User> users = this.forumController.getAllUsers();
			this.forumfrm.setVectorUser(users);
			ans+=this.sendPlayerMsg(nameOfUser,this.forumfrm);
		}
		
		
		
		
		else if (msg.getData().startsWith("get messages"))
		{
			//int i = 1;
			try
			{
				Vector<Message> msgs=this.forumController.getMessages(getNameOfForum(str), getNameOfThread(str));
				forumfrm.setVectorMsg(msgs);
				
				ans+=this.sendPlayerMsg(nameOfUser,forumfrm );
			}
			catch (RuntimeException e) 
			{
				this.InvalidMessage(nameOfUser);

			}
		}
		
		else if (msg.getData().startsWith("get threads"))
		{
			try
			{
				Vector <Trd> t = this.forumController.getThreads(getNameOfForum(str));
				forumfrm.setVectorTrd(t);
				ans+=this.sendPlayerMsg(nameOfUser,forumfrm);
			}
			catch (RuntimeException e) 
			{
				this.InvalidMessage(nameOfUser);
				
			}
		}
		else if (msg.getData().startsWith("get forums"))
		{
			try
			{
				Vector <Frm> f = this.forumController.getForums();
				//System.out.println("Forum Vector size: " + f.size());
				this.forumfrm.setVectorFrm(f);
				ans+=this.sendPlayerMsg(nameOfUser,forumfrm);
				
			}
			catch (RuntimeException e) 
			{
				this.InvalidMessage(nameOfUser);
			}
		}
		else if  (msg.getData().startsWith("get type of user"))
		{
			//this.forumfrm.setTypeOfUser(nameOfUser, this.forumController.getTypeOfUser(nameOfUser));
/*			int typeOfUser = this.forumController.getTypeOfUser(nameOfUser);
			if(typeOfUser==0){
				ans = "default";
			}else if(typeOfUser==1){
				ans = "manager";
			}*/
			this.forumfrm.setTypeOfUser(nameOfUser, this.forumController.getTypeOfUser(nameOfUser));  
			   ans+=this.sendPlayerMsg(nameOfUser,this.forumfrm);
		}
		else if (msg.getData().startsWith("get friends of user"))
		{
			Vector <User> u = this.forumController.getFriendsOfUser(nameOfUser);
			if (u == null)
			{
				this.InvalidMessage(nameOfUser);
			}
			else {
				this.forumfrm.setVectorFriends(u);
				ans+=this.sendPlayerMsg(nameOfUser,this.forumfrm);
			}
		}
		return ans;
	}
	
	
}
